<?php
session_start();
require_once("registereduserobj.php");
if (isset($_SESSION["REGISTEREDUSER"])) {unset($_SESSION["REGISTEREDUSER"]);}
$obj = new registereduser();
$obj->applyfromrequest();
if ($obj->valid) {
 if ($obj->writedb()) {
   $obj->addgroup("UNACTIVATED");
   $obj->activationemail() ;
   $obj->unload();
   confirmfiled();
   unset($obj);
   unset($_SESSION["captcha"]);
 } else {
  showpage($obj);
 }
} else {
 showpage($obj);
}
exit;

function confirmfiled() {
 echo "Please check your email and follow the link to activate your account";
}

function showpage($obj) {
 // Write out a captcha
  $im = imagecreate(82, 20) or die("Cannot Initialize new GD image stream");
  $background_color = imagecolorallocate($im, 223,234,248);
  $text_color = imagecolorallocate($im, 76,147,233);
  $p = generatepassword(); 
  $_SESSION["captcha"] = md5($p);
  imagestring($im, 5,4, 0,  $p, $text_color); 
  $im = imagerotate($im,mt_rand(0,89)-44,$background_color);
  $fn = "captcha\\".md5($p).".png";
  imagepng($im,$fn);
  imagedestroy($im);

 $i = $obj->header;
 
 echo "<h1>User Setup</h1>";
 echo "<form name='form' method=get>";
 echo "<table>";
 
 echo "<tr>";
 echo "<td>Email</td>";
 echo "<td>";
 echo "<input name=f9 value='$i[9]'>";
 echo "</td>";
 echo "<td>";
 echo $obj->valids[9];
 echo "</td>";
 echo "</tr>";

 echo "<td>Name</td>";
 echo "<td>";
 echo '<input name=f1 value="'.$i[1].'">';
 echo "</td>";
 echo "<td>";
 echo $obj->valids[1];
 echo "</td>";
 echo "</tr>";

 echo "<tr>";
 echo "<td>Password</td>";
 echo "<td>";
 echo "<input type=password name=f5>";
 echo "</td>";
 echo "</tr>";

 echo "<tr>";
 echo "<td>Password (again)</td>";
 echo "<td>";
 echo "<input type=password name=i5>";
 echo "</td>";
 echo "<td>";
 echo $obj->valids[5];
 echo "</td>";
 echo "</tr>";

 echo "<tr>";
 echo "<td align=center>Word verification.<br> Type the characters you see below.<br>";
 echo "<img src='$fn'>";
 echo "</td>";
 echo "<td>";
 echo "<input name=i8>";
 echo "</td>";
 echo "<td>";
 echo $obj->valids[8];
 echo "</td>";
 echo "</tr>";

 echo "</table>";
 echo "<input type=submit>";
 echo "</form>"; 
 }
?>